#include "TCPStream.h"
#include "Exception.h"
#include <Windows.h>
#include <WinSock.h>

TCPStream::TCPStream(int sock, bool alive)
{
	_sock = sock;
	_alive = alive;
}

TCPStream::~TCPStream(void)
{
}

void TCPStream::Write(char* buffer, int offset, int length)
{
	if(_alive)
	{
		send(_sock, buffer+offset, length, 0);
	}
	else
	{
		throw Exception("invalid stream");
	}
}

void TCPStream::Flush()
{
	if(_alive)
	{
	}
	else
	{
		throw Exception("invalid stream");
	}
}

int TCPStream::Read(char* buffer, int offset, int length)
{
	if(_alive)
	{
		int bytesRead = recv(_sock, buffer + offset, length, 0);
		return bytesRead;
	}
	else
	{
		throw Exception("invalid stream");
	}
	return 0;
}

void TCPStream::BeginRead()
{

}

void TCPStream::EndRead()
{

}

void TCPStream::Dispose()
{
	_alive = false;
}